#include "musicslider.h"
#include "ui_musicslider.h"
#include<QMouseEvent>
MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    // 初始情况下，还没有开始播放，将当前播放进度设置为0
     currentPos = 0;
     maxWidth = width();
     moveSilder();

}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    // 注意：QMouseEvent中的pos()为⿏标相对于widget的坐标，不是相当于screen
     // 因此⿏标位置的 x 坐标可直接作为outLine的宽度
     currentPos = event->pos().x();
     moveSilder();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    // 如果⿏标不在MusicSlider的矩形内，不进⾏拖拽
     QRect rect = QRect(0, 0, geometry().width(), geometry().height());
     QPoint pos = event->pos();
     if(!rect.contains(pos))
     {
        return;
     }

    currentPos=event->pos().x();
    if(event->buttons()==Qt::LeftButton)
    {
        if(currentPos<0)
        {
            currentPos=0;
        }
        if(currentPos>maxWidth)
        {
            currentPos=maxWidth;
        }
        moveSilder();

    }
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSilder();
    emit setMusicSliderPosition(ui->outLine->width() / (float)maxWidth);
}

void MusicSlider::moveSilder()
{
    // 根据当前进度设置外部滑动条的位置
     ui->outLine->setMaximumWidth(currentPos);
     ui->outLine->setGeometry(0,8, currentPos,4);
}

void MusicSlider::setStep(float bf)
{
    currentPos = maxWidth*bf;
     moveSilder();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}
